<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码压缩工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: #333;
            text-align: center;
        }

        textarea {
            width: 100%;
            min-height: 150px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-family: monospace;
        }

        #output-code {
            background-color: #f9f9f9;
        }

        button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>代码压缩工具</h1>
        <textarea id="input-code" placeholder="在此粘贴需要压缩的代码..."></textarea>
        <button id="compress-btn">压缩代码</button>
        <textarea id="output-code" readonly placeholder="压缩后的代码将显示在这里..."></textarea>
    </div>
    <script>
        document.getElementById('compress-btn').addEventListener('click', function () {
            const input = document.getElementById('input-code').value;
            let output = input
                // 移除多行注释
                .replace(/\/\*[\s\S]*?\*\//g, '')
                // 移除单行注释
                .replace(/\/\/.*$/gm, '')
                // 移除多余的空格和换行
                .replace(/\s+/g, ' ')
                // 移除语句前后的空格
                .replace(/([;{}()=+,:])\s+/g, '$1')
                .replace(/\s+([;{}()=+,:])/g, '$1')
                // 特殊处理字符串内容中的空格
                .replace(/"\s+(.*?)\s+"/g, '"$1"')
                .replace(/'\s+(.*?)\s+'/g, '\'$1\'')
                // 最终整理
                .trim();

            document.getElementById('output-code').value = output;
        });
    </script>
</body>

</html>
</write_file>